/*
 * steering.c
 *
 *  Created on: 2012.12.02.
 *      Author: Peter
 */
#include "stm32f4xx.h"
#include "steering.h"


void InitSteerPeriphs(void)
{
	  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	  TIM_OCInitTypeDef TIM_OCInitStructure;
	  GPIO_InitTypeDef GPIO_InitStructure;

	  TIM_TimeBaseStructInit(&TIM_OCInitStructure);
	  TIM_OCStructInit(&TIM_OCInitStructure);
	  GPIO_StructInit(&GPIO_InitStructure);

	  /* TIM1 clock enable */
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

	  /* GPIOA clock enable */
	  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

	  /* GPIOC Configuration: TIM1 CH1 (PA8) */
	  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8  ;
	  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
	  GPIO_Init(GPIOA, &GPIO_InitStructure);


	  /* Connect TIM1 pins to AF2 */
	  GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);

	  /* Time base configuration */
	  TIM_TimeBaseStructure.TIM_Period = PWM_PERIOD_STEERING;
	  TIM_TimeBaseStructure.TIM_Prescaler = PWM_STERRING_PRESC;
	  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

	  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

	  /* PWM1 Mode configuration: Channel1 */
	  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
	  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	  TIM_OCInitStructure.TIM_Pulse = PWM_STEERING_CCR1;
	  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

	  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
	  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);
	  /* Enable TIM1 Preload register on ARR */
	  TIM_ARRPreloadConfig(TIM1, ENABLE);
	  /* TIM1 enable counter */
	  TIM_Cmd(TIM1, ENABLE);
	  /* TIM1 Main Output Enable */
	  TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

void SetSteeringPWM(int16_t angle)
{
	uint16_t pwm;
	pwm = angle + PWM_STEERING_CCR1;
	TIM1->CCR1 = pwm;
}

